package com.dems.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dems.dao.dataobject.CfgArea;
import com.dems.dao.dataobject.ResRegion;
import com.dems.dao.dataobject.User;
import com.dems.dao.mapper.la.CfgAreaMapper;
import com.dems.dao.mapper.la.ResRegionMapper;
import com.dems.domain.RegionReqVO;
import com.dems.service.ResRegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ResRegionServiceImpl implements ResRegionService {
    @Autowired
    private ResRegionMapper resRegionMapper;

    @Autowired
    private CfgAreaMapper cfgAreaMapper;

    @Override
    public int createRegion(ResRegion resRegion, User user) {
        resRegion.setCreateTime(new Date());
        resRegion.setCreateUserId(user.getUserId());
        int result = resRegionMapper.insertSelective(resRegion);
        resRegion = resRegionMapper.selectByRegionName(resRegion.getRegionName());
        CfgArea cfgArea = new CfgArea();
        cfgArea.setNodeId(resRegion.getId());
        cfgArea.setNodeLevel(3);
        cfgArea.setNodeName(resRegion.getRegionName());
        cfgArea.setParentId(resRegion.getCityId());
        cfgArea.setNodeType("REGION");
        result = cfgAreaMapper.insertSelective(cfgArea);
        return result;
    }

    @Override
    public int updateRegion(ResRegion resRegion,User user) {
        resRegion.setUpdateUserId(user.getUserId());
        resRegion.setUpdateTime(new Date());
        return resRegionMapper.updateByPrimaryKeySelective(resRegion);
    }

    @Override
    public Page queryRegionPageInfo(RegionReqVO regionReqVO) {
        Page page = new Page(regionReqVO.getPageNum(), regionReqVO.getPageSize());
        ResRegion resRegion = new ResRegion();
        BeanUtils.copyProperties(regionReqVO, resRegion);
        return resRegionMapper.queryRegionList(page, resRegion);
    }
}
